package cn.audit.controller.user;

import cn.audit.aop.myannotation.BussAnnotation;
import cn.audit.bean.dto.BatchProcessing;
import cn.audit.bean.pojo.AuditInstitution;
import cn.audit.bean.pojo.RoleManager;
import cn.audit.bean.pojo.User;
import cn.audit.bean.dto.UserCorrelation;
import cn.audit.service.institution.InstitutionService;
import cn.audit.service.role.RoleService;
import cn.audit.service.user.UserService;
import cn.audit.utils.EmptyUtils;
import cn.audit.utils.MD5;
import cn.audit.utils.PagerUtil;
import cn.audit.utils.Paging;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 李忆枫
 * @Date: 2019/5/18 15:30
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private InstitutionService institutionService;

    @Resource
    private RoleService roleService;

    @RequestMapping("/userAdmin")
    public String toUserAdmin(Model model,
                              @RequestParam(value = "page", required = false) Integer page,
                              @RequestParam(value = "department", required = false) Integer department) {

        int page1 = 1;
        if (page != null) {
            page1 = page;
        }

        //用户
        List<UserCorrelation> userAll = userService.findUserAll(department);

        PagerUtil pu = Paging.findBook(page1, userAll);
        model.addAttribute("userAll", pu);
        //部门
        List<AuditInstitution> institutions = institutionService.findAll();
        model.addAttribute("institutions", institutions);
        //角色
        List<RoleManager> roleList = roleService.findAll();
        model.addAttribute("roleList", roleList);
        //当前页数
        model.addAttribute("pager", page1);

        return "User/UserAdmin";
    }

    @ResponseBody
    @RequestMapping("/AjaxVerifyUserName")
    public Boolean AjaxVerifyUserName(String username) {
        User user = userService.findByUsername(username);

        if (EmptyUtils.isNotEmpty(user)) {
            return false;
        }
        return true;
    }


    @RequestMapping("/userAdd")
    @BussAnnotation(moduleName = "用户管理", type = "添加", option = "添加用户成功")
    public String UserAdd(String username, String name,
                          Integer department, Integer userRole,
                          Integer post, String userAptitude, String password) {

        User user = new User(username, MD5.string2MD5(password), name, post.longValue(), 1L, userAptitude, null, null, department.longValue(), userRole.longValue());
        userService.save(user);

        return "redirect:userAdmin";
    }

    /**
     * 根据id查找User
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/AjaxUserById")
    public Map<String, Object> AjaxUserById(Integer id) {
        //创建一个map，用来封装数据
        Map adminMap = new HashMap(16);
        User byId = userService.findById(id.longValue());
        adminMap.put("byId", byId);

        return adminMap;
    }


    /**
     * 修改
     *
     * @param user
     * @return
     */
    @RequestMapping("/userUpdate")
    @BussAnnotation(moduleName = "用户管理", type = "修改", option = "修改用户成功")
    public String userUpdate(User user) {
        user.setStatus(1L);
        userService.save(user);

        return "redirect:userAdmin";
    }

    /**
     * 批量删除
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/AjaxDelete")
    @BussAnnotation(moduleName = "用户管理", type = "删除", option = "删除用户成功")
    public Boolean AjaxDelete(BatchProcessing ids,Long status) {
        List<Long> id = ids.getIds();
        userService.batchUpdate(id,status);
        return true;
    }


    /**
     * 重置密码
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/AjaxResetPwd")
    @BussAnnotation(moduleName = "用户管理", type = "重置密码", option = "重置密码成功")
    public Boolean AjaxResetPwd(BatchProcessing ids) {
        List<Long> id = ids.getIds();
        userService.batchResetPwd(MD5.string2MD5("123456"), id);
        return true;
    }

}
